<?php
error_reporting(E_ALL ^ E_NOTICE);
ini_set('memory_limit', '1000M');
set_time_limit(0);
CONST TYPE_NORMAL = 1;
CONST TYPE_BAD = 2;
CONST ETL_TYPE = 'EXIST';
#CONST ETL_TYPE = 'NEW';
include_once('function.php');
include_once('sell.php');
include_once('rent.php');
$borough_file_name = 'borough';
$broker_check_file_name = 'broker_check';
$borough_price_file_name = 'borough_price';
$borough_file=file($borough_file_name);



function field_is_null($house_info){
    if (ETL_TYPE == 'NEW'){
        $house_info['created'] = time();
        $house_info['updated'] = time();
    }
    if(($house_info['borough_name']=="" || empty($house_info['borough_name'])) && ($house_info['house_room']=="" || empty($house_info['house_room'])) && ($house_info['house_topfloor']=="" || empty($house_info['house_topfloor']))&& ($house_info['house_price']=="" || empty($house_info['house_price'])) && ($house_info['cityarea_id']=="" || empty($house_info['cityarea_id'])) && ($house_info['cityarea2_id']=="" || empty($house_info['cityarea2_id'])) && ($house_info['off_type']=="" || empty($house_info['off_type'])) ){
        $house_info['bad_type'] = 1;
    }
    return $house_info;
}
function rent_key_word_filter($house_info, $rent_config){
    //整租 房源标题过滤合租关键词
    foreach($rent_config['ARRAY_HEZU_IN_RENT_KW'] as $keyword){
        $result = strpos($house_info['house_title'],$keyword);
        $result = strval($result);
        if($result != false || $result === "0"){
	    echo $house_info['id']."\t".$keyword."\t".$house_info['house_title']."\n";
            $house_info['bad_type'] = 18;
            return $house_info;
        }
    }
    //整租 房源描述过滤合租关键词
    $skip_hezu = False;
    foreach($rent_config['ARRAY_HEZU_NOTIN_RENT_DESC_KW'] as $keyword){
        $result = strpos($house_info['house_desc'],$keyword);
        $result = strval($result);
        if($result != false || $result === "0"){
            $skip_hezu = True;
        }
    }
    if (!$skip_hezu){
        foreach($rent_config['ARRAY_HEZU_IN_RENT_DESC_KW'] as $keyword){
            $result = strpos($house_info['house_desc'],$keyword);
            $result = strval($result);
            if($result != false || $result === "0"){
        	echo $house_info['id']."\t".$keyword."\t".$house_info['house_desc']."\n";        
                $house_info['bad_type'] = 19;
                return $house_info;
            }
        }
    }
    return $house_info;
}
function hadleFields($house_info, $rent_config){
    //小区名不能为空，为空直接扔掉
    $regex='/[a-z]+:\/\/[a-z0-9_\-\/.%]+/i';
    $matched=preg_match($regex, $house_info['source_url']);
    if ($matched == 0 and !startwith($house_info['source_url'], 'www')){
        $house_info['bad_type'] = 36;
        return $house_info;
    }
    if (empty($house_info['borough_name']) || $house_info['borough_name']==""){
       $house_info['bad_type'] = 4;
       return $house_info;
    }

    //房屋面积异常
    if (empty($house_info['house_totalarea']) || $house_info['house_totalarea']==0){
        if($house_info['source']!=10 ){
            $house_info['bad_type'] = 5;
            return $house_info;
        }
    }
    else{
        $pattern='/^[.0-9]{1,10}$/';
        if(!preg_match($pattern, $house_info['house_totalarea'])){
            $house_info['bad_type'] = 5;
            return $house_info;
        }
    }
    if($house_info['house_totalarea']<0 || $house_info['house_totalarea']>50000){
        $house_info['bad_type'] = 5;
        return $house_info;
    }
    $house_info['house_pic_unit'] = trim($house_info['house_pic_unit']);
    $house_info['house_pic_layout'] = trim($house_info['house_pic_layout']);
    if (!empty($house_info['house_pic_unit'])){
        if (!startwith($house_info['house_pic_unit'], 'http') and !startwith($house_info['house_pic_unit'], '|http')){
            $house_info['bad_type'] = 37;
        }
    }
    if (!empty($house_info['house_pic_layout'])){
        if (!startwith($house_info['house_pic_layout'], 'http') and !startwith($house_info['house_pic_unit'], '|http')){
            $house_info['bad_type'] = 38;
        }
    }

    //室数据异常(查询gov表中总室数最大值，20)
    if(trimall($house_info['house_room'])<0 || trimall($house_info['house_room'])>=21){
        $house_info['bad_type'] = 6;
        return $house_info;
    }
    //总楼层小于0扔掉
    if(trimall($house_info['house_topfloor'])<0 || trimall($house_info['house_topfloor'])>=66){
        $house_info['bad_type'] = 7;
  	    return $house_info;
    }
    //出租价格小于等于0扔掉
    if(trimall($house_info['house_price'])<=0 || trimall($house_info['house_price'])>1000000){
        $house_info['bad_type'] = 8;
	    return $house_info;
    }
    //过滤租房类型不匹配关键字
    $house_info = rent_key_word_filter($house_info, $rent_config);
    if($house_info['bad_type']){
        return $house_info;
    }
    //厕所如果为空的话 默认值为1
    if(empty($house_info['house_toilet']) || $house_info['house_toilet']==""){
        $house_info['house_toilet']=1;
    }
        //卫数据异常(查询gov表中总楼层最大值，10)
    if (trimall($house_info['house_toilet']) < 1 or trimall($house_info['house_toilet']) > 11){
        $house_info['bad_type'] = 32;
        return $house_info;
    }
    //厅数据异常(查询gov表中总楼层最大值，11)
    if (trimall($house_info['house_hall']) < 0 or trimall($house_info['house_hall']) > 12){
        $house_info['bad_type'] = 33;
        return $house_info;
    }
    //对官网电话做统一处理
    if(strstr($house_info['owner_phone'],"-")){
        $house_info['owner_phone'] = str_replace("-","",$house_info['owner_phone']);
    }
    if(strstr($house_info['owner_phone'],"转")){
        $house_info['owner_phone'] = str_replace("转", ",",$house_info['owner_phone']);
    }
    return $house_info;
}
function hadleFieldsPersonal($house_info, $sell_config, $rent_config, $broker_check){
    //小区名不能为空，为空直接扔掉
    if((empty($house_info['borough_name']) || trimall($house_info['borough_name'])=="")&&(empty($house_info['cityarea_id']) || trimall($house_info['cityarea_id'])=="") && (empty($house_info['cityarea2_id']) || trimall($house_info['cityarea2_id'])=="") ){
        $house_info['bad_type'] = 14;
        return $house_info;
        
     }
    //检测标题关键字
    foreach($sell_config['ARRAY_COMPANY_KW'] as $keyword){
        $result = strpos($house_info['house_title'],$keyword);
        $result = strval($result);
        if($result != false || $result === "0"){
            $result = strpos($house_info['borough_name'],$keyword);
            $result = strval($result);
            if($result != false || $result === "0"){
                continue;
            }
            $house_info['bad_type'] = 16;
            return $house_info;
        }
    }
    //检测房源描述关键字
    foreach($sell_config['ARRAY_COMPANY_KW'] as $keyword){
        $result = strpos($house_info['house_desc'],$keyword);
        $result = strval($result);
        if($result != false || $result === "0"){
            $result = strpos($house_info['borough_name'],$keyword);
            $result = strval($result);
            if($result != false || $result === "0"){
                continue;
            }
            $house_info['bad_type'] = 17;
            return $house_info;
        }
    }
    //检测标题关键字
    foreach($rent_config['ARRAY_RENT_KW'] as $keyword){
        $result = strpos($house_info['house_title'],$keyword);
        $result = strval($result);
        if($result != false || $result === "0"){
            $house_info['bad_type'] = 21;
            return $house_info;
        }
    }
    //检测房源描述关键字
    foreach($rent_config['ARRAY_RENT_KW'] as $keyword){
        $result = strpos($house_info['house_desc'],$keyword);
        $result = strval($result);
        if($result != false || $result === "0"){
            $house_info['bad_type'] = 22;
            return $house_info;
        }
    }
    //过滤租房类型不匹配关键字
    $house_info=rent_key_word_filter($house_info, $rent_config);
    if($house_info['bad_type']){
	return $house_info;
    }
    //判定 房源标题  中 关键字 标注为转租
    foreach($rent_config['ARRAY_IS_RELET'] as $keyword){
        $result = strpos($house_info['house_title'],$keyword);
        $result = strval($result);
        if($result != false || $result === "0"){
            $house_info['house_relet'] = 1;
            return $house_info;
        }
    }
    //判定 房源描述  中 关键字 标注为转租
    foreach($rent_config['ARRAY_IS_RELET'] as $keyword){
        $result = strpos($house_info['house_desc'],$keyword);
        $result = strval($result);
        if($result != false || $result === "0"){
            $house_info['house_relet'] = 1;
            return $house_info;
        }
    }
    //经纪人过滤
    if(!empty($broker_check[$house_info['owner_phone']])){
        $house_info['bad_type'] = 15;
        return $house_info;
    }
    return $house_info;
}
function outputBadHouse($house_info, $bad_type){
    #if ($bad_type=='A'){
    #}
    $house_info['bad_type'] = $bad_type;
    $out = json_encode($house_info);
    echo $house_info['source_url']."\t".TYPE_BAD."\t".$out."\n";
}
function delHtml($value, $sell_config){
    $data = str_replace($sell_config['DEL_HTML'], '', strip_tags($value));
    return $data;
}
function normalizeEmptyValue($house_info, $rent_config){
    $check_empty_key=array('owner_phone', 'service_phone', 'borough_name',
        'house_fitment', 'house_toward', 'house_floor', 'house_totalarea', 'house_built_year');
    if ($house_info['owner_phone'] == ','){
        $house_info['owner_phone'] = "";
    }
    if ($house_info['service_phone'] == ','){
        $house_info['service_phone'] = "";
    }
    $house_info['house_room'] = trim($house_info['house_room']);
    $house_info['house_topfloor'] = trim($house_info['house_topfloor']);
    foreach ($check_empty_key as $key){
        $house_info[$key] = trim($house_info[$key]);
        foreach ($rent_config['ARRAY_EMPTY_CONFIG'] as $empty_str){
            if ($house_info[$key] == $empty_str){
                $house_info[$key] = '';
            }
        }
    }
    return $house_info;
}
function mergeOne($house_info, $rent_config, $borough_info_tbl){
    //电话为空的话 直接返回官网电话
    if($house_info['owner_phone']=="" || empty($house_info['owner_phone'])){
        if($house_info['source']!=10){
            if($house_info['service_phone'] == "" || empty($house_info['service_phone'])){
                $govtel = $rent_config['ARRAY_GOV_TEL'];
                $house_info['service_phone'] = $govtel[$house_info['source']];
            }
        }
     }
    //室/居中文数字转换为阿拉伯数字
    if($house_info['house_room']){
        foreach ($rent_config['ARRAY_HALL_CONFIG'] as $inkey=>$val){
            if(preg_match("/.*$val.*/",trimall($house_info['house_room']))){
                $house_info['house_room'] = $inkey;
                break;
            }
        }
    }
    //厅转换
    if($house_info['house_hall']){
        $house_info['house_hall'] = delHtml($house_info['house_hall'], $sell_config);
        foreach ($rent_config['ARRAY_HALL_CONFIG'] as $inkey=>$val){
            if(preg_match("/.*$val.*/",trimall($house_info['house_hall']))){
                $house_info['house_hall'] = $inkey;
                break;
            }
        }
    }
    //装修情况
    $matched=False;
    if($house_info['house_fitment']){
        $house_info['house_fitment'] = delHtml($house_info['house_fitment'], $sell_config);
        foreach ($rent_config['ARRAY_FITMENT_CONFIG'] as $inkey=>$val){
            if(preg_match("/.*$val.*/",trimall($house_info['house_fitment']))){
                $house_info['house_fitment'] = $inkey;
                $matched=True;
                break;
            }
        }
        if (ETL_TYPE == 'NEW'){
            if(!$matched){
                $house_info['bad_type'] = 29;
            }
        }
        else{
            if(!$matched and ($house_info['house_fitment'] <1 or $house_info['house_fitment'] > 8)){
                $house_info['bad_type'] = 29;
            } 
        }
    }    
    //朝向
    $matched=False;
    if($house_info['house_toward']){
        $house_info['house_toward'] = delHtml($house_info['house_toward'], $sell_config);
        foreach ($rent_config['ARRAY_TOWARD_CONFIG'] as $inkey=>$val){
            if(preg_match("/.*$inkey.*/",trimall($house_info['house_toward']))){
                $house_info['house_toward'] = $val;
                $matched=True;
                break;
            }
        }
        if (ETL_TYPE == 'NEW'){
            if(!$matched){
                $house_info['bad_type'] = 28;
            }
        }
        else{
            if(!$matched and ($house_info['house_toward'] < 1 or $house_info['house_toward'] > 18)){
                $house_info['bad_type'] = 28;
            }
        }
    }

    //房源类型
    if($house_info['house_type']){
        $house_info['house_type'] = delHtml($house_info['house_type'], $sell_config);
        foreach ($rent_config['ARRAY_HOUSETP_CONFIG'] as $inkey=>$val){
            if(preg_match("/.*$val.*/",trimall($house_info['house_type']))){
                $house_info['house_type'] = $inkey;
                break;
            }
        }
    }
    //房屋楼层 高 中 低,判断是不是数字
    if($house_info['house_floor']){
        $house_info['house_floor'] = delHtml($house_info['house_floor'], $rent_config);
        foreach ($rent_config['ARRAY_HFLOOR_CONFIG'] as $inkey=>$val){
            if(preg_match("/.*$val.*/",trimall($house_info['house_floor']))){
                $house_info['house_floor'] = $inkey;
                break;
            }
        }
        if(!in_array($house_info['house_floor'],array('高', '中', '低'))){
            $pattern = '/[^\x00-\x80]/';
            if (preg_match($pattern, $house_info['house_floor'])){
                $house_info['bad_type'] = 31;
            }
            else if ($house_info['house_floor'] <=-4 or $house_info['house_floor']>=66){
                $house_info['bad_type'] = 31;
            }
        }
    }
    
    //tag 标签
    if (ETL_TYPE == 'NEW'){
        if(isset($house_info['tag']) or $house_info['tag']=='0' or $house_info['tag']==0){
            $house_info['tag'] = "";
	    $tag=trimall($house_info['tag']);
	    $house_info['tag']='#';
            foreach ($rent_config['ARRAY_PAYM_TAG'] as $val=>$inkey){
                if(preg_match("/.*$val.*/",$tag)){
                    $house_info['tag'] .= $inkey.'#';
                }
            }
        }
	else{
	    $house_info['tag']='#';
	}
    }
    //房屋主次卧出租
    if($house_info['house_style']){
        foreach ($rent_config['ARRAY_HSTYLE_CONFIG'] as $inkey=>$val){
            if(preg_match("/.*$val.*/",trimall($house_info['house_style']))){
                $house_info['house_style'] = $inkey;
                break;
            }
        }
    }
    //标题判断主卧次卧
    if($house_info['house_title']){
        if(empty($house_info['house_style'])){
            $house_info['house_style'] = '';
        }
        foreach ($rent_config['ARRAY_HSTYLE_CONFIG'] as $inkey=>$val){
            if(preg_match("/.*$val.*/",trimall($house_info['house_title']))){
                $house_info['house_style'] = $inkey;
                break;
            }
        }
    }
    //卧室室内配置
    $cfroom = trimall($house_info['house_configroom']);
    if($cfroom){
        $configInter = $rent_config['ARRAY_INTER_CONFIG'];
        $configroom = explode("#",$cfroom);
        $temp_cfroom ="";
        foreach($configroom as $val){
            $temp_cfroom.=$configInter[$val].'#';
        }
        if($temp_cfroom!=""){
            $house_info['house_configroom']=$temp_cfroom;
        }
    }
    //公共配置
    $cfpub = trimall($house_info['house_configpub']);
    if($cfpub){
        $configOuter = $rent_config['ARRAY_OUTER_CONFIG'];
        $configpub = explode("#",$cfpub);
        $temp_cfpub ="";
        foreach($configpub as $val){
            $temp_cfpub .=$configOuter[$val].'#';
        }
        if($temp_cfpub!=""){
            $house_info['house_configpub']=$temp_cfpub;
        }
    }
    //住租要求
    if($house_info['sex']){
        $limsex = $rent_config['ARRAY_LIMITSEX_CONFIG'];
        foreach ($limsex as $inkey=>$val){
            if(preg_match("/.*$inkey.*/",trimall($house_info['sex']))){
                $house_info['sex'] = $val;
                break;
            }
        }
    }
    //付款方式 ：例如押一付三
    if($house_info['pay_type']){
        $pay_type = $rent_config['ARRAY_PAYM_CONFIG'];
        foreach ($pay_type as $inkey=>$val){
            if(preg_match("/.*$val.*/",trim($house_info['pay_type']))){
                $house_info['pay_type'] = $inkey;
                break;
            }
        }
    }
    //房源描述如果为空的话
    if(empty($house_info['house_desc']) || $house_info['house_desc']==""){
        $borough_info = $borough_info_tbl[$house_info['borough_id']];
        $borough_desc = array();
        if($borough_info){
            $borough_desc["附近有超市商场"] = $borough_info['borough_shop'];
            $borough_desc["附近有医院"] = $borough_info['borough_hospital'];
            $borough_desc["附近有学校"] = implode(" ",array($borough_info['nursery_school'],$borough_info['middle_school'],$borough_info['elementary_school']));
        }
        if($house_info['source'] == 10){
            $house_desc = "我是业主，我的房子在".$house_info['borough_name'].",";
            foreach($borough_desc as $desc=>$desc_data){
                $desc_data=trim($desc_data);
                if(!empty($desc_data)){
                    $house_desc = $house_desc.$desc.",".$desc_data.",";
                }
            }
            $house_desc .= "希望把房子交给有缘人。";
        }else{
            $sourcename = $rent_config["ARRAY_GOV_SOURCE"];
            $house_desc = "我是".$sourcename[$house_info['source']]."的一名经纪人，竭诚为您服务，这套房子在".$house_info['borough_name']."。";
            foreach($borough_desc as $desc=>$desc_data){
                $desc_data=trim($desc_data);
                if(!empty($desc_data)){
                    $house_desc = $house_desc.$desc.",".$desc_data.",";
                }
            }
            $house_desc .= "我保证信息的真实，并期待您和我联系!";
        }
        $house_info['house_desc'] = $house_desc;
    }
    return $house_info;
}
$borough = array();
$borough_info = array();
$broker_check = array();
$borough_price = array();
$borough_file = file($borough_file_name);
#$borough_price_file = file($borough_price_file_name);
foreach($borough_file as &$line_json){
    $line = json_decode($line_json, True);
    $borough[$line['borough_name']] = 
	array('id'=>$line['id'], 'cityarea_id'=>$line['cityarea_id'], 'cityarea2_id'=>$line['cityarea2_id']);
}
$borough_info_keys = array('borough_shop', 'middle_school', 'elementary_school', 'nursery_school', 'borough_hospital');
/*foreach($broker_check_file as &$line_json){
    $line = json_decode($line_json, True);
    $broker_check[$line['phone']] = $line['id'];
}*/

$borough_price_str = file_get_contents($borough_price_file_name);
$borough_price = json_decode($borough_price_str, True);

#$gov_filename='house_sell_gov_test';
#$gov_file=file($gov_filename);
#foreach($gov_file as &$line_json){
while (($line_json = fgets(STDIN)) !== false) {
    $line_json = trim($line_json);
    if(empty($line_json))
        continue;
    $house_info = json_decode($line_json, true);
    $house_info['borough_name'] = trimall($house_info['borough_name']);
    $house_info = field_is_null($house_info);
    if(!empty($house_info['bad_type'])){
	outputBadHouse($house_info, $house_info['bad_type']);
	continue;
    }
    $house_info = normalizeEmptyValue($house_info, $rent_config);
    if($house_info['source']==10)
	    $house_info = hadleFieldsPersonal($house_info, $sell_config, $rent_config, $broker_check);
    else{
        if (strstr($house_info['owner_phone'], "<")){
	      $house_info['owner_phone'] = '';
	    }
        if(!empty($house_info['owner_phone']) && empty($broker_check[$house_info['owner_phone']]) ){
            echo "broker_check"."\t".$house_info['owner_phone']."\t".$house_info['source']."\n";
        }
    }
    $house_info = hadleFields($house_info, $rent_config);
    if(!empty($house_info['bad_type'])){
        outputBadHouse($house_info, $house_info['bad_type']); 
	continue;
    }
    //处理小区
    if (ETL_TYPE == 'NEW'){
        $house_info['tag'] = str_replace("#12#", "#", $house_info['tag']);
    }
    if ($borough[$house_info['borough_name']]){
	    $house_info['borough_id'] = $borough[$house_info['borough_name']]['id'];
	    $house_info['cityarea_id'] = $borough[$house_info['borough_name']]['cityarea_id'];
	    $house_info['cityarea2_id'] = $borough[$house_info['borough_name']]['cityarea2_id'];	
        if (ETL_TYPE == 'NEW'){
            if ($borough[$house_info['borough_name']]['subway']=='1'){
                $house_info['tag'] = $house_info['tag'].'12#';
            }
        }
    }
    else{
	    outputBadHouse($house_info, 20);
	    continue;
    }
    $house_info['tag'] = str_replace("#1001#", "#", $house_info['tag']);
    $house_info['tag'] = str_replace("#1002#", "#", $house_info['tag']);
    if ($house_info['house_totalarea'] > 0){
        $hprice=$house_info['house_price']/$house_info['house_totalarea'];
        if ($hprice > 1000 || $hprice < 10){
            outputBadHouse($house_info, 34);
            continue;
        }
        if ($borough_price[$house_info['borough_id']]){
            $bprice=$borough_price[$house_info['borough_id']];
            $price_ratio =$house_info['house_price']/$house_info['house_totalarea'] / $bprice;
            if ($price_ratio > 2 || $price_ratio < 0.5){
                $house_info['tag'] = $house_info['tag']."1001#";
            }
        }
    }
    //过滤Q房网
    if (ETL_TYPE=='NEW'){
        if($house_info['source'] == 5){
        $res = preg_match('/Q房网/u',$house_info['company']);
            if(!$res){
                outputBadHouse($house_info, 13);
                continue;
            }
            else if(strlen($house_info['company']) < 15) {
                outputBadHouse($house_info, 13);
                continue;
            }
        }
    }
    $house_info = mergeOne($house_info, $rent_config, $borough_info_tbl);
    if(!empty($house_info['bad_type'])){
        outputBadHouse($house_info, $house_info['bad_type']);
        continue;
    }
    $pattern = '/[^\x00-\x80]/';
    if(empty($house_info['house_title']) || !preg_match($pattern,$house_info['house_title'])){
        $house_info['house_title'] = $house_info['borough_name']." ";
        $need_space=False;
        if(!empty($house_info['house_room'])){
            $house_info['house_title'] = $house_info['house_title'].$house_info['house_room']."室";
            $need_space=True;
        }
        if(!empty($house_info['house_hall'])){
            $need_space=True;
            $house_info['house_title'] = $house_info['house_title'].$house_info['house_hall']."厅";
        }
        if(!empty($house_info['house_toilet'])){
            $need_space=True;
            $house_info['house_title'] = $house_info['house_title'].$house_info['house_toilet']."卫";
        }
        if($need_space)
            $house_info['house_title'] = $house_info['house_title']." ";
        if(!empty($house_info['house_fitment']))
            $house_info['house_title'] = $house_info['house_title'].$rent_config['ARRAY_FITMENT_CONFIG2'][$house_info['house_fitment']]." ";
        if(!empty($house_info['house_toward']))
            $house_info['house_title'] = $house_info['house_title'].$rent_config['ARRAY_TOWARD_CONFIG'][$house_info['house_toward']];
        $house_info['house_title'] = trim($house_info['house_title']);
    }

    $out = json_encode($house_info);
    echo $house_info['source_url']."\t".TYPE_NORMAL."\t".$out."\n";
}
    

    
